﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xoohoo.Infrastructure;
using Xoohoo.Extensions;
using Xoohoo.Models;
using Xoohoo.Routing;
using Xoohoo.Infrastructure.InversionOfControl;
using Xoohoo.Models.InputModels;
using Xoohoo.FilterProviders.FilterCriterion;
using Xoohoo.Modules.CMS.Controllers;
using Xoohoo.ActionFilters.Authorization;
using System.Web.Mvc;

namespace Xoohoo.Modules.CMS
{
    public class CMSModule : Module, IModuleMetaData
    {

        #region Module Members

        public override string ModuleName
        {
            get { return "CMS"; }
        }

        public override string AuthenticationModuleName
        {
            get { return "Admin"; }
        }

        public override void RegisterRoutes()
        {

            string[] controllerNamespaces = new string[] { "Xoohoo.Modules.CMS.Controllers" };

            #region 菜单

            MapRoute(
                "Admin.CMS.SubMenu",
                "CMS/SubMenu",
                new { controller = "CMS", action = "SubMenu" },
                null,
                controllerNamespaces
            );

            #endregion

            #region 文章管理

            //文章列表
            MapRoute(
                "Admin.CMS.Article.List",
                "CMS/Article/{pageSize}/{pageNumber}",
                new { controller = "CMS", action = "ArticleList", pageSize = 10, pageNumber = 1 },
                new { pageSize = new IsInt(1, Int32.MaxValue), pageNumber = new IsInt(1, Int32.MaxValue) },
                controllerNamespaces
                );

            //文章编辑
            MapRoute(
                "Admin.CMS.Article.Edit",
                "CMS/Article/Edit/{articleID}",
                new { controller = "CMS", action = "ArticleAddOrEdit" },
                new { articleID = new IsGuid() },
                controllerNamespaces
                );

            //文章添加
            MapRoute(
                "Admin.CMS.Article.Add",
                "CMS/Article/Add",
                new { controller = "CMS", action = "ArticleAddOrEdit" },
                null,
                controllerNamespaces
                );

            //文章保存
            MapRoute(
                "Admin.CMS.Article.Save",
                "CMS/Article/Save/{articleID}",
                new { controller = "CMS", action = "ArticleSave" },
                new { articleID = new IsNullableGuid() },
                controllerNamespaces
                );

            //文章删除
            MapRoute(
                "Admin.CMS.Article.Remove",
                "CMS/Article/Remove/{articleID}",
                new { controller = "CMS", action = "ArticleRemove" },
                new { articleID = new IsGuid() },
                controllerNamespaces
                );

            #endregion

            #region 栏目管理

            //栏目列表
            MapRoute(
                "Admin.CMS.Category.List",
                "CMS/Category",
                new { controller = "CMS", action = "CategoryList" },
                null,
                controllerNamespaces
                );

            //栏目编辑
            MapRoute(
                "Admin.CMS.Category.Edit",
                "CMS/Category/Edit/{categoryID}",
                new { controller = "CMS", action = "CategoryAddOrEdit" },
                new { categoryID = new IsGuid() },
                controllerNamespaces
                );

            //栏目添加
            MapRoute(
                "Admin.CMS.Category.Add",
                "CMS/Category/Add",
                new { controller = "CMS", action = "CategoryAddOrEdit" },
                null,
                controllerNamespaces
                );

            //栏目保存
            MapRoute(
                "Admin.CMS.Category.Save",
                "CMS/Category/Save/{categoryID}",
                new { controller = "CMS", action = "CategorySave" },
                new { categoryID = new IsNullableGuid() },
                controllerNamespaces
                );

            //文章删除
            MapRoute(
                "Admin.CMS.Category.Remove",
                "CMS/Category/Remove/{categoryID}",
                new { controller = "CMS", action = "CategoryRemove" },
                new { categoryID = new IsGuid() },
                controllerNamespaces
                );

            #endregion

            #region 链接管理

            MapRoute(
                "Admin.CMS.Link.List",
                "CMS/Link/{pageSize}/{pageNumber}",
                new { controller = "CMS", action = "LinkList", pageSize = 10, pageNumber = 1 },
                new { pageSize = new IsInt(1, Int32.MaxValue), pageNumber = new IsInt(1, Int32.MaxValue) },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Link.Add",
                "CMS/Link/Add",
                new { controller = "CMS", action = "LinkAddOrEdit" },
                null,
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Link.Edit",
                "CMS/Link/Edit/{linkID}",
                new { controller = "CMS", action = "LinkAddOrEdit" },
                new { linkID = new IsGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Link.Save",
                "CMS/Link/Save/{linkID}",
                new { controller = "CMS", action = "LinkSave", linkID = UrlParameter.Optional },
                new { linkID = new IsNullableGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Link.Move",
                "CMS/Link/Move/{linkID}/{target}",
                new { controller = "CMS", action = "LinkMove" },
                new { linkID = new IsGuid(), target = new IsMovingTarget() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Link.Remove",
                "CMS/Link/Remove/{linkID}",
                new { controller = "CMS", action = "LinkRemove" },
                new { linkID = new IsGuid() },
                controllerNamespaces
                );

            #endregion

            #region 链接分类管理

            MapRoute(
                "Admin.CMS.LinkCategory.List",
                "CMS/LinkCategory",
                new { controller = "CMS", action = "LinkCategoryList" },
                null,
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.LinkCategory.Add",
                "CMS/LinkCategory/Add",
                new { controller = "CMS", action = "LinkCategoryAddOrEdit" },
                null,
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.LinkCategory.Edit",
                "CMS/LinkCategory/Edit/{linkCategoryID}",
                new { controller = "CMS", action = "LinkCategoryAddOrEdit" },
                new { linkCategoryID = new IsGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.LinkCategory.Save",
                "CMS/LinkCategory/Save/{linkCategoryID}",
                new { controller = "CMS", action = "LinkCategorySave", linkCategoryID = UrlParameter.Optional },
                new { linkCategoryID = new IsNullableGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.LinkCategory.Move",
                "CMS/LinkCategory/Move/{linkCategoryID}/{target}",
                new { controller = "CMS", action = "LinkCategoryMove" },
                new { linkCategoryID = new IsGuid(), target = new IsMovingTarget() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.LinkCategory.Remove",
                "CMS/LinkCategory/Remove/{linkCategoryID}",
                new { controller = "CMS", action = "LinkCategoryRemove" },
                new { linkCategoryID = new IsGuid() },
                controllerNamespaces
                );


            #endregion

            #region 基础数据管理

            MapRoute(
                "Admin.CMS.Basicdata.CommendStatus.List",
                "CMS/Basicdata/CommendStatus",
                new { controller = "CMS", action = "CommendStatusList" },
                null,
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.CommendStatus.Add",
                "CMS/Basicdata/CommendStatus/Add",
                new { controller = "CMS", action = "CommendStatusAddOrEdit" },
                null,
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.CommendStatus.Edit",
                "CMS/Basicdata/CommendStatus/Edit/{commendStatusID}",
                new { controller = "CMS", action = "CommendStatusAddOrEdit" },
                new { commendStatusID = new IsGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.CommendStatus.Save",
                "CMS/Basicdata/CommendStatus/Save/{commendStatusID}",
                new { controller = "CMS", action = "CommendStatusSave", commendStatusID = UrlParameter.Optional },
                new { publishStatusID = new IsNullableGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.CommendStatus.Move",
                "CMS/Basicdata/CommendStatus/Move/{commendStatusID}/{target}",
                new { controller = "CMS", action = "CommendStatusMove" },
                new { commendStatusID = new IsGuid(), target = new IsMovingTarget() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.CommendStatus.Remove",
                "CMS/Basicdata/CommendStatus/Remove/{commendStatusID}",
                new { controller = "CMS", action = "CommendStatusRemove" },
                new { commendStatusID = new IsGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.List",
                "CMS/Basicdata/PublishStatus",
                new { controller = "CMS", action = "PublishStatusList" },
                null,
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.Add",
                "CMS/Basicdata/PublishStatus/Add",
                new { controller = "CMS", action = "PublishStatusAddOrEdit" },
                null,
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.Edit",
                "CMS/Basicdata/PublishStatus/Edit/{publishStatusID}",
                new { controller = "CMS", action = "PublishStatusAddOrEdit" },
                new { publishStatusID = new IsGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.Save",
                "CMS/Basicdata/PublishStatus/Save/{publishStatusID}",
                new { controller = "CMS", action = "PublishStatusSave", publishStatusID = UrlParameter.Optional },
                new { publishStatusID = new IsNullableGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.Move",
                "CMS/Basicdata/PublishStatus/Move/{publishStatusID}/{target}",
                new { controller = "CMS", action = "PublishStatusMove" },
                new { publishStatusID = new IsGuid(), target = new IsMovingTarget() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.Remove",
                "CMS/Basicdata/PublishStatus/Remove/{publishStatusID}",
                new { controller = "CMS", action = "PublishStatusRemove" },
                new { publishStatusID = new IsGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.SetDefault",
                "CMS/Basicdata/PublishStatus/SetDefault/{publishStatusID}",
                new { controller = "CMS", action = "PublishStatusSetDefault" },
                new { publishStatusID = new IsGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.SetPublished",
                "CMS/Basicdata/PublishStatus/SetPublished/{publishStatusID}",
                new { controller = "CMS", action = "PublishStatusSetPublished" },
                new { publishStatusID = new IsGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.SetItemCanBeRemove",
                "CMS/Basicdata/PublishStatus/SetItemCanBeRemove/{publishStatusID}/{itemCanBeRemove}",
                new { controller = "CMS", action = "PublishStatusSetItemCanBeRemove" },
                new { publishStatusID = new IsGuid(), itemCanBeRemove = new IsBoolean() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.PublishStatus.SetItemCanBeEdit",
                "CMS/Basicdata/PublishStatus/SetItemCanBeEdit/{publishStatusID}/{itemCanBeEdit}",
                new { controller = "CMS", action = "PublishStatusSetItemCanBeEdit" },
                new { publishStatusID = new IsGuid(), itemCanBeEdit = new IsBoolean() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.TopStatus.List",
                "CMS/Basicdata/TopStatus",
                new { controller = "CMS", action = "TopStatusList" },
                null,
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.TopStatus.Add",
                "CMS/Basicdata/TopStatus/Add",
                new { controller = "CMS", action = "TopStatusAddOrEdit" },
                null,
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.TopStatus.Edit",
                "CMS/Basicdata/TopStatus/Edit/{topStatusID}",
                new { controller = "CMS", action = "TopStatusAddOrEdit" },
                new { topStatusID = new IsGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.TopStatus.Save",
                "CMS/Basicdata/TopStatus/Save/{topStatusID}",
                new { controller = "CMS", action = "TopStatusSave", topStatusID = UrlParameter.Optional },
                new { topStatusID = new IsNullableGuid() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.TopStatus.Move",
                "CMS/Basicdata/TopStatus/Move/{topStatusID}/{target}",
                new { controller = "CMS", action = "TopStatusMove" },
                new { topStatusID = new IsGuid(), target = new IsMovingTarget() },
                controllerNamespaces
                );

            MapRoute(
                "Admin.CMS.Basicdata.TopStatus.Remove",
                "CMS/Basicdata/TopStatus/Remove/{topStatusID}",
                new { controller = "CMS", action = "TopStatusRemove" },
                new { topStatusID = new IsGuid() },
                controllerNamespaces
                );

            #endregion
        }

        public override void RegisterFilters(FilterProviders.FilterRegistryFilterProvider filterRegistry, System.Web.Mvc.GlobalFilterCollection globalFilters)
        {
            #region 模块权限

            ControllerTypeFilterCriteria cmsControllerCriteria = new ControllerTypeFilterCriteria();
            cmsControllerCriteria.AddType<CMSController>();
            filterRegistry.Add(new[] { cmsControllerCriteria }, new AdvancedAuthorizationFilter(DependencyInjector, user => user.HasPermission("内容管理")));

            #endregion

            #region 菜单权限

            //由于设置了整个CMSController的Filter,菜单权限就可以不设置了
            //ControllerActionFilterCriteria subMenuCriteria = new ControllerActionFilterCriteria();
            //subMenuCriteria.AddMethod<CMSController>(s => s.SubMenu());
            //filterRegistry.Add(new[] { subMenuCriteria }, new AdvancedAuthorizationFilter(_dependencyInjector, user => user.HasPermission("内容管理")));

            #endregion
        }

        #endregion

        #region IModuleMetaData Members

        /// <summary>
        /// 获取后台管理菜单
        /// </summary>
        /// <returns></returns>
        public List<ModuleMenuItem> GetModuleMenus()
        {
            return new List<ModuleMenuItem>
            {
                new ModuleMenuItem
                {
                    Title = "内容管理",//主菜单标题
                    SubMenuRouteName = "Admin.CMS.SubMenu",//子菜单路由名称
                    ContentRouteName = "Admin.CMS.Article.List", //内容页路由名称
                    Permission = "内容管理"
                }
            };
        }

        /// <summary>
        /// 获取模块可以被分配权限
        /// </summary>
        /// <returns></returns>
        public List<PermissionInput> GetModulePermissions()
        {
            var permissions = new List<PermissionInput>()
            {
               new PermissionInput(){ PermissionID = new Guid("{FD1000B1-A4A3-48C4-8BAD-40B5691FFE54}"), PermissionName="内容管理", ModuleName= this.ModuleName}
            };

            return permissions;
        }

        #endregion

    }
}
